package cn.udrm.water.base.v1.manager.restful;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.udrm.water.base.v1.commons.models.PageModel;
import cn.udrm.water.base.v1.commons.models.RequestListParam;
import cn.udrm.water.base.v1.commons.models.ResponseParam;
import cn.udrm.water.base.v1.commons.models.ZTreeNodeVo;
import cn.udrm.water.base.v1.manager.dto.BaseDepartmentDto;
import cn.udrm.water.base.v1.manager.dto.BasePersonDto;
import cn.udrm.water.base.v1.manager.dto.BaseRoleDto;
import cn.udrm.water.base.v1.manager.service.BaseDepartmentService;
import cn.udrm.water.base.v1.manager.service.BaseRoleService;

@Api(tags="部门实体API", description="部门CRUD操作")
@RestController
@RequestMapping(value="v1/restful/department")
public class BaseDepartmentRestful {
	@Autowired
	private BaseDepartmentService managerUnitService;
	@Autowired
	private BaseRoleService roleService;
	
	@ApiOperation("列表数据获取")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="body", name="requestListParam", required=false, value="列表查询条件", dataType="ResponseParam«BaseDepartmentDto»")
	})
	@RequestMapping(value="list", method={RequestMethod.GET, RequestMethod.POST})
	public ResponseParam<PageModel<BaseDepartmentDto>> list(@RequestBody(required=false) RequestListParam<BaseDepartmentDto> requestListParam) {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<BaseDepartmentDto> pageByConditions = managerUnitService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<BaseDepartmentDto>>("数据获取成功", 200, pageByConditions);
	}
	
	@GetMapping(value="getWithAllById/{id}")
	public ResponseParam<BaseDepartmentDto> getWithAllById(@PathVariable(value="id") String id) {
		BaseDepartmentDto withAllById = managerUnitService.getWithAllById(id);
		return new ResponseParam<BaseDepartmentDto>("数据获取成功", 200, withAllById);
	}
	
	@ApiOperation("单条数据获取")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="管理单位GUID")
	@GetMapping(value="get/{id}")
	public ResponseParam<BaseDepartmentDto> get(@PathVariable(value="id") String id) {
		BaseDepartmentDto baseDepartmentDto = managerUnitService.getSelfById(id);
		return new ResponseParam<BaseDepartmentDto>("数据获取成功", 200, baseDepartmentDto);
	}
	
	@PostMapping(value="getWithChildrens")
	public ResponseParam<List<ZTreeNodeVo>> getWithChildrens(@RequestParam(value="id") String id, @RequestParam(value="type") String type) {
		ArrayList<ZTreeNodeVo> list = new ArrayList<ZTreeNodeVo>();
		switch (type) {
			case "DEPARTMENT":
				BaseDepartmentDto withChildrens = managerUnitService.getWithChildrens(id);
				if (withChildrens != null && withChildrens.getChildrensDto() != null) {
					List<BaseDepartmentDto> childrensDto = withChildrens.getChildrensDto();
					childrensDto.forEach((dept) -> {
						list.add(new ZTreeNodeVo(dept.getId(), dept.getName(), id, "DEPARTMENT"));
					});
				}
				BaseDepartmentDto withPersons = managerUnitService.getWithPersons(id);
				if (withPersons != null && withPersons.getPersonDtos() != null) {
					List<BasePersonDto> personDtos = withPersons.getPersonDtos();
					personDtos.forEach((p) -> {
						list.add(new ZTreeNodeVo(p.getId(), p.getName(), id, "PERSON"));
					});
				}
				return new ResponseParam<List<ZTreeNodeVo>>("数据获取成功", 200, list);
			case "ROLE":
				BaseRoleDto baseRoleDto = roleService.getWithPersons(id);
				if (baseRoleDto != null && baseRoleDto.getPersonDtos() != null) {
					List<BasePersonDto> personDtos = baseRoleDto.getPersonDtos();
					personDtos.forEach((p) -> {
						list.add(new ZTreeNodeVo(p.getId(), p.getName(), id, "PERSON"));
					});
				}
				return new ResponseParam<List<ZTreeNodeVo>>("数据获取成功", 200, list);
		}
		return new ResponseParam<List<ZTreeNodeVo>>("传入type未找到", 501);
	}
	
	@GetMapping(value="getWithRoles/{id}")
	public ResponseParam<BaseDepartmentDto> getWithRoles(@PathVariable(value="id") String id) {
		BaseDepartmentDto withRoles = managerUnitService.getWithRoles(id);
		return new ResponseParam<BaseDepartmentDto>("数据获取成功", 200, withRoles);
	}
	
	@ApiOperation("管理单位数据保存")
	@ApiImplicitParam(name="BaseDepartmentDto", value="需要添加的管理单位", required=true, dataType="BaseDepartmentDto", dataTypeClass=BaseDepartmentDto.class)
	@PostMapping(value="save")
	public ResponseParam<BaseDepartmentDto> save(@RequestBody BaseDepartmentDto baseDepartmentDto) {
		baseDepartmentDto = managerUnitService.save(baseDepartmentDto);
		
		return new ResponseParam<BaseDepartmentDto>("数据保存成功", 200, baseDepartmentDto);
	}
	
	@ApiOperation("管理单位数据更新")
	@ApiImplicitParam(name="BaseDepartmentDto", value="需要更新的管理单位", required=true, dataType="BaseDepartmentDto", dataTypeClass=BaseDepartmentDto.class)
	@PostMapping(value="update")
	public ResponseParam<BaseDepartmentDto> update(@RequestBody BaseDepartmentDto baseDepartmentDto) {
		baseDepartmentDto = managerUnitService.update(baseDepartmentDto);
		
		return new ResponseParam<BaseDepartmentDto>("数据更新成功", 200, baseDepartmentDto);
	}
	
	@ApiOperation("管理单位数据删除（仅删除自身，不能删除标识有实体间关系）")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="管理单位GUID")
	@GetMapping(value="deleteForce/{id}")
	public ResponseParam<String> deleteForce(@PathVariable(value="id") String id) {
		try {
			managerUnitService.deleteAllSelf(new String[]{id});
		} catch (Exception e) {
			return new ResponseParam<String>("删除失败", 500);
		}
		return new ResponseParam<String>("删除成功", 200);
	}
}
